package modelo.dao.jpa;

import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import modelo.bean.GostoMusical;

public class JPAGostoMusicalDAO {

	private static EntityManagerFactory factory;

	public JPAGostoMusicalDAO() {
		factory = JPAFactory.getInstanceManagerFactory();
	}

	public void insert(GostoMusical gostoMusical) throws Exception {
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.persist(gostoMusical);
		em.getTransaction().commit();
		em.close();
	}

	public void update(GostoMusical gostoMusical) throws Exception {
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		em.merge(gostoMusical);
		em.getTransaction().commit();
		em.close();
	}

	public void delete(GostoMusical gostoMusical) throws Exception {
		EntityManager em = factory.createEntityManager();
		em.remove(gostoMusical);
		em.close();
	}

	public GostoMusical consultaGostoMusical(GostoMusical gostoMusical)
			throws Exception {
		EntityManager em = factory.createEntityManager();
		GostoMusical u = em.find(GostoMusical.class,
				gostoMusical.getId_gostomusical());
		em.close();
		return u;
	}

	@SuppressWarnings("unchecked")
	public Collection<GostoMusical> listaGostoMusicals(String condicao)
			throws Exception {
		EntityManager em = factory.createEntityManager();
		Collection<GostoMusical> GostoMusicals = em.createQuery(condicao)
				.getResultList();
		em.close();
		return GostoMusicals;
	}

	public void destroyFactory() {
		factory.close();
	}

}
